<template>
  <div class="wrap">
    <!-- <div class="title">修改当前管理员账户信息</div> -->
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="auto"
      class="ruleForm-wrap"
    >
      <el-form-item label="管理员" prop="user_wanted">
        <el-input
          v-model="ruleForm.user_wanted"
          :disabled="true"
          minlength="5"
          message
          placeholder=""
          type="text"
        />
      </el-form-item>
      <el-form-item label="场馆代码" prop="site_code">
        <el-input
          v-model="ruleForm.site_code"
          :disabled="true"
          minlength="5"
          message
          placeholder=""
          type="text"
        />
      </el-form-item>
      
      <el-form-item class="btn-wrap">
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          设置
        </el-button>
        <el-button type="warning" plain @click="resetForm(ruleFormRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted, getCurrentInstance } from 'vue'
import { ElMessage } from 'element-plus'
import { postData } from '@/http/request.js'
import { useRouter, useRoute } from 'vue-router'
let router = useRouter()
let route = useRoute()
const { proxy } = getCurrentInstance()

let ruleFormRef = ref(null)
const user_name = route.query.id


const ruleForm = reactive({
  user_wanted: user_name,
  site_code: '',
})

const rules = reactive({
 
})

let getInit = () => {
  postData('manage/admin/extra/info', {
    user_wanted: user_name,
  })
    .then((res) => {
      console.log(res)
      if (res.code == 0) {
        let datas = res.datas
        Object.assign(ruleForm, datas)
        console.log(ruleForm)
       
      } else if (res.code == -56055) {
        return
      } else {
        ElMessage({
          type: 'error',
          message: res.code + '，' + res.msg,
        })
      }
    })
    .catch((err) => {
      console.log(err)
      ElMessage({
        type: 'error',
        message: '请稍后重试',
      })
    })
}



const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      postData('manage/admin/extra/set', {
        user_wanted: ruleForm.user_wanted,
        user_pwd: ruleForm.user_pwd,
        nick_name: ruleForm.nick_name,
        mobile: ruleForm.mobile,
        email: ruleForm.email,
        token_expired: Number(ruleForm.token_expired),
        roles: ruleForm.selRole,
      })
        .then((res) => {
          if (res.code == 0) {
            ElMessage({
              type: 'success',
              message: '修改成功',
            })
            setTimeout(() => {
              router.push('/adminList')
            }, 3000)
          } else {
            ElMessage({
              type: 'error',
              message: '修改失败',
            })
          }
        })
        .catch((err) => {
          console.log(err)
        })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}

onMounted(() => {
  getInit()
  
})
</script>
<style scope>
.wrap {
  margin: 20px 0 0 20px;
}

.title {
  width: 100%;
  height: 40px;
  line-height: 40px;
  position: relative;
  /* background: #c0dee2; */
  padding-left: 30px;
  font-size: 14px;
  margin: 6px 0 20px 0;
  /* border-radius: 12px 12px 0 0; */
  /* font-weight: bold; */
}

.title::before {
  content: '';
  width: 2px;
  height: 16px;
  background: #676a6d;
  position: absolute;
  top: 12px;
  left: 15px;
}

.title span {
  float: left;
  color: #676a6d;
  margin-left: 25px;
}

.btn-wrap {
  margin-top: 30px;
}
</style>
